package cn.xshi.sys.web;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseSearch;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.sys.model.SysVersion;
import cn.xshi.sys.service.SysVersionService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
/**
 * @Desc 平台版本
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/sysVersion")
@Api(value = "平台版本API",tags = "平台版本API",description = "平台版本API")
public class SysVersionController extends BaseAction {

	@Resource
	private SysVersionService sysVersionService;

	/**
	 * 查询平台版本列表并分页
	 * @param baseSearch
	 */
	@PostMapping(value="/list")
	@Auth(value = "/sysVersion/list",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	@ApiOperation(value="查询平台版本列表并分页", notes="查询平台版本列表并分页")
	public BasePage<List<SysVersion>> getSysVersionListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		commonHPager(baseSearch);
		List<SysVersion> sysVersionList = sysVersionService.getSysVersionListByCondition(condition);
		for(SysVersion sysVersion : sysVersionList){
			if(!StringUtil.isEmpty(sysVersion.getCreateId())){
				OauthAccountEntity createBy = getAccount(sysVersion.getCreateId());
				if(null != createBy){
					sysVersion.setCreateBy(createBy.getName());
				}
			}
			if(!StringUtil.isEmpty(sysVersion.getUpdateId())){
				OauthAccountEntity modifiedBy = getAccount(sysVersion.getUpdateId());
				if(null != modifiedBy){
					sysVersion.setModifiedBy(modifiedBy.getName());
				}
			}
		}
		PageInfo<SysVersion> page = new PageInfo<SysVersion>(sysVersionList);
		return outPageBootStr(page,baseSearch);
	}

	/**
	 * 查询单个平台版本
	 * @param id
	 */
	@GetMapping(value="/get/{id}")
	@Auth(value = "/sysVersion/get",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	@ApiOperation(value="查询单个平台版本", notes="查询单个平台版本")
	public BaseResult<SysVersion> getSysVersionById(@PathVariable("id")String id){
		SysVersion sysVersion = sysVersionService.getSysVersionById(id);
		if(!StringUtil.isEmpty(sysVersion.getCreateId())){
			OauthAccountEntity createBy = getAccount(sysVersion.getCreateId());
			if(null != createBy){
				sysVersion.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(sysVersion.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(sysVersion.getUpdateId());
			if(null != modifiedBy){
				sysVersion.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(sysVersion);
	}

	/**
	 * 添加
	 * @param sysVersion
	 */
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个平台版本", notes="创建单个平台版本")
	public BaseResult addSysVersion(@RequestBody SysVersion sysVersion){
		int i = 0;
		if(null != sysVersion){
			sysVersion.setId(toUUID());
			i= sysVersionService.addSysVersion(sysVersion);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 修改
	 * @param sysVersion
	 */
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个平台版本", notes="编辑单个平台版本")
	public BaseResult updateSysVersion(@RequestBody SysVersion sysVersion){
		int i = 0;
		if(null != sysVersion){
			i= sysVersionService.updateSysVersion(sysVersion);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 删除
	 * @param id
	 */
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除平台版本", notes="删除平台版本")
	public BaseResult delSysVersion(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i= sysVersionService.delSysVersion(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
}

